package com.fametal.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import com.fametal.R;

public class CustomSeekBar extends LinearLayout implements OnClickListener {
	private SeekBar mSeekBar;
	private OnClickListener listener;
	public static final int INCREASE = 1;
	public static final int DECREASE = 2;
	private int mMax;
	private int mMin;
	
	public CustomSeekBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		View contentView = LayoutInflater.from(context).inflate(R.layout.custom_seekbar, null);
		Button increase = (Button) contentView.findViewById(R.id.increase);
		increase.setOnClickListener(this);
		Button decrease = (Button) contentView.findViewById(R.id.decrease);
		decrease.setOnClickListener(this);
		mSeekBar = (SeekBar) contentView.findViewById(R.id.custom_seekbar);
		mSeekBar.setEnabled(false);
		addView(contentView);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.increase:
			if(listener.onClick(this, getProgress() + 1 , INCREASE));
				Log.d("david", "getProgress = " + getProgress());
				setProgress(getProgress() + 1);
			break;
		case R.id.decrease:
			if(listener.onClick(this, getProgress() - 1, DECREASE))
				Log.d("david", "getProgress = " + getProgress());
				setProgress(getProgress() - 1);
			break;
		}
	}
	
	public void setScope(int max, int min) {
		this.mMin = min;
		this.mMax = max;
		mSeekBar.setMax(Math.abs(max) + Math.abs(min));
	}
	
	public int getProgress(){
		return mSeekBar.getMax() - mSeekBar.getProgress() - Math.abs(mMin);
	}
	
	public void setProgress(int progress) {
		mSeekBar.setProgress(mSeekBar.getMax() - (progress + Math.abs(mMin)));
	}
	
	public void setOnClickListener(OnClickListener listener){
		this.listener = listener;
	}
	
	public interface OnClickListener{
		public boolean onClick(View view, int progress, int mode);
	}
}
